Skip to content

opens new pool after calling migrator close to avoid sql: database is…#2040

Merged
timbastin merged 1 commit into
mainfrom
fix-migration-retry
May 21, 2026
Merged

opens new pool after calling migrator close to avoid sql: database is…#2040
timbastin merged 1 commit into
mainfrom
fix-migration-retry

Conversation

@timbastin
Copy link
Copy Markdown
Member

… closed error - retry of failed migrations works now

… closed error - retry of failed migrations works now
Copilot AI review requested due to automatic review settings May 21, 2026 15:13
@timbastin timbastin merged commit 8a3a953 into main May 21, 2026
13 checks passed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to make failed migrations retryable by resetting schema_migrations using a fresh DB connection after migrator.Close() (which closes the underlying *sql.DB). It also introduces a new migration that removes indexes/columns from component_dependencies and attempts to enforce deduplication at the schema level.

Changes:

  • Update migration runner to create a fresh pool/DB for resetting schema_migrations after a failed migration.
  • Add a SQL migration that drops obsolete indexes/columns on component_dependencies, de-dupes rows, and replaces the surrogate PK with a composite primary key.

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 3 comments.

File Description
database/migrations/20260521095426_remove_obsolete_indexes_and_columns_from_component_dependencies.up.sql Drops columns/indexes and replaces component_dependencies PK with a composite PK after deduplication.
database/migrations.go On migration failure, closes migrator and uses a new pool to reset schema_migrations dirty/version state.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread database/migrations.go
Comment on lines 82 to 86
// Release the migrator's connection (advisory lock + any open tx) before
// touching schema_migrations on the same pool — with MaxOpenConns=1 this
// would otherwise deadlock.
// touching schema_migrations — migrator.Close() also closes the underlying
// sql.DB it was given, so we need a fresh connection for the reset.
migrator.Close()
// clear dirty flag and restore version so the migration can be retried — safe in postgres since DDL is transactional
Comment thread database/migrations.go
Comment on lines +87 to +93
resetCfg := GetPoolConfigFromEnv()
resetCfg.MaxOpenConns = 1
resetCfg.MinConns = 0
resetDB := NewGormDB(NewPgxConnPool(resetCfg))
if resetSQLDB, dbErr := resetDB.DB(); dbErr == nil {
defer resetSQLDB.Close()
if _, err = resetSQLDB.Exec("UPDATE schema_migrations SET dirty = false, version = $1", versionBefore); err != nil {
Comment thread database/migrations.go
if _, err = resetSQLDB.Exec("UPDATE schema_migrations SET dirty = false, version = $1", versionBefore); err != nil {
monitoring.Alert("failed to reset migration state after failed migration", err)
}
slog.Info("successfully reset migration state - feel free to try again")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrations should never create the dirty flag in the schema_migrations table

2 participants